<?php
include_once("../../includes/metodos.php");

function insert(){
	connectSql();
	
	$categoria = anti_injection($_POST['categoria']);
	$query = mysql_query("SELECT nome FROM econ_categorias WHERE nome='$categoria'");
	$row_nome = mysql_fetch_array($query);
	if($row_nome['nome'] == $categoria){
		echo '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
							alert ("Categoria existente!");
							location.href="new.php";
					  </SCRIPT>';
	}
	$sql = mysql_query("INSERT INTO econ_categorias VALUES (null , '".$categoria."', NOW(), NOW())");
	if(!$sql){
		echo 'Ocorreu um problema: '.mysql_error();
	}else{
		echo '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">location.href="index.php"; </SCRIPT>';
	}
}

function edit($id){
	connectSql();
	
	$categoria = anti_injection($_POST['categoria']);
	$sql = mysql_query("SELECT nome FROM econ_categorias WHERE nome='$categoria'");
	$row_nome = mysql_fetch_array($sql);
	if($row_nome['nome'] == $categoria){
		echo '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
								alert ("Categoria existente!");
								location.href="new.php";
						  </SCRIPT>';
	}
	$query = mysql_query("UPDATE econ_categorias SET nome='$categoria', updated_at=NOW() WHERE id=".anti_injection($id));
	if(!$query){
		echo 'Ocorreu um problema: '.mysql_error();
	}else{
		header('Location: index.php');
	}
}

function remove($id){
	$query = mysql_query("DELETE FROM econ_categorias WHERE id=".anti_injection($id));
	echo '<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
								location.href="index.php";
						  </SCRIPT>';
}